<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterialUnit extends Model
{
    use HasFactory;

    public $timestamps = false;

    protected $fillable = [
        'keyword_one_id',
        'keyword_two_id',
        'book_id',
        'paint_id',
        'natural_spelling_id',
        'video_id',
        'music_id',
        'test_id',
    ];

    public static function boot()
    {
        parent::boot();

        static::created(function (self $item) {
            // 增加当前教材单元数
            $item->materials->increment("number");
        });
        static::deleted(function (self $item) {
            // 增加当前教材单元数
            $item->materials->decrement("number");
        });
    }

    public function materials()
    {
        return $this->belongsTo(TeachingMaterial::class, 'book_id');
    }
}
